/**
 * Created by Administrator on 2018/5/20.
 */
var Square = function () {

    this.data = [
        [0, 0, 2, 0],
        [0, 0, 2, 0],
        [0, 0, 2, 0],
        [0, 0, 2, 0]
    ];
    //    原点
    this.origin = {
        x: 0,
        y: 0
    };
    //方向
    this.dir = 0;
//    旋转数组
    this.rotates = [
        [
            [0, 0, 1, 0],
            [0, 0, 1, 0],
            [0, 0, 1, 0],
            [0, 0, 1, 0]
        ],
        [
            [0, 0, 0, 0],
            [1, 1, 1, 1],
            [0, 0, 0, 0],
            [0, 0, 0, 0]
        ],
        [
            [0, 0, 1, 0],
            [0, 0, 1, 0],
            [0, 0, 1, 0],
            [0, 0, 1, 0]
        ],
        [
            [0, 0, 0, 0],
            [1, 1, 1, 1],
            [0, 0, 0, 0],
            [0, 0, 0, 0]
        ]
    ]
};

/*
 * 旋转
 * */

Square.prototype.canRotate = function (isValid) {
    var d = this.dir + 1;
    if(d==4){
        d = 0;
    }
    var test = [
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ];
    for(var i = 0; i< this.data.length; i++){
        for(var j = 0; j < this.data[0].length; j++){
            console.log(this.rotates[d][i][j]);
            test[i][j] = this.rotates[d][i][j];
        }
    }
    console.log(JSON.stringify(test));
    return isValid(this.origin,test);

};

Square.prototype.rotate = function () {
    this.dir = this.dir + 1;
    if(this.dir == 4){
        this.dir = 0;
    }

    for(var i = 0; i< this.data.length; i++){
        for(var j = 0; j < this.data[0].length; j++){
            this.data[i][j] = this.rotates[this.dir][i][j];

        }
    }
};




/*
* 移动
* */

//上移
Square.prototype.canup = function (isValid) {
    var test = {};
    test.x = this.origin.x - 1;
    test.y = this.origin.y;
    return isValid(test, this.data);
};
Square.prototype.up = function(){
    this.origin.x = this.origin.x - 1;
};
//下移
Square.prototype.canDown = function (isValid) {
    var test = {};
    test.x = this.origin.x + 1;
    test.y = this.origin.y;
    return isValid(test, this.data);
};
Square.prototype.down = function () {
    this.origin.x = this.origin.x + 1;
};
//右移
Square.prototype.canLeft = function (isValid) {
    var test = {};
    test.x = this.origin.x;
    test.y = this.origin.y - 1;
    return isValid(test, this.data);
};
Square.prototype.right = function () {
    this.origin.y = this.origin.y + 1;
};
//左移
Square.prototype.canRight = function (isValid) {
    var test = {};
    test.x = this.origin.x;
    test.y = this.origin.y + 1;
    return isValid(test, this.data);
};
Square.prototype.left = function () {
    this.origin.y = this.origin.y - 1;
};

